package com.yx.shop.service.impl;

import com.yx.shop.dao.ProductDao;
import com.yx.shop.dao.impl.ProductDaoImpl;
import com.yx.shop.entitly.PageBean;
import com.yx.shop.entitly.Product;
import com.yx.shop.service.ProductService;
import com.yx.shop.utils.JDBCUtils;

import java.util.List;

/**
 * @Classname ProductServiceImpl
 * @Description TODO:
 * @Date 2021/05/07 17:18
 * @Created by yx
 */
public class ProductServiceImpl implements ProductService {
    private ProductDao productDao = new ProductDaoImpl();
    @Override
    public List<Product> getByTid(PageBean page, Integer tid) {
        List<Product> productList = null;
        try {
            JDBCUtils.begin();
            productList= productDao.getProductByTid(page, tid);
            JDBCUtils.commit();
        } catch (Exception e) {
            JDBCUtils.rollback();
            e.printStackTrace();
        }
        return productList;
    }

    @Override
    public int getCount(Integer tid) {
        int count = 0;
        try {
            JDBCUtils.begin();
            if (tid>0){
                //tid是一个正常值
                count = productDao.getCount(tid).intValue();
            }
            JDBCUtils.commit();
        } catch (Exception e) {
            JDBCUtils.rollback();
            e.printStackTrace();
        }
        return count;
    }

    @Override
    public Product getProductById(Integer pid) {
        Product product = null;
        try {
            JDBCUtils.begin();
            product = productDao.getById(pid);
            JDBCUtils.commit();
        } catch (Exception e) {
            JDBCUtils.rollback();
            e.printStackTrace();
        }
        return product;
    }
}
